from django.http import Http404, HttpResponseRedirect, HttpResponse
from django.shortcuts import render_to_response, get_object_or_404
from django.template import RequestContext
from django.contrib.auth import logout, login, authenticate
from django.contrib.auth.models import User
from django.contrib.auth.decorators import login_required
from django.contrib.auth.forms import PasswordChangeForm
from datetime import datetime

def GetUser(user_name):
    try:
        return User.objects.get(username=user_name)
    except User.DoesNotExist:
        raise Http404('Requested user not found.')

@login_required
def account(request, user_name):
    user = GetUser(user_name)
    variables = RequestContext(request, {
        'user_public' : user,
    })
    return render_to_response('registration/account.html', variables)

def change_password(request, user_name):
    if request.method == 'POST':
        form = PasswordChangeForm(user=request.user, data=request.POST)
        if form.is_valid():
            form.save()
            return HttpResponseRedirect('/user/'+ user_name + '/')
    else:
        form = PasswordChangeForm(user=request.user)
    variables = RequestContext(request, { 'form': form })
    return render_to_response('registration/password_change.html', variables)